/*
*    UNIVERSIDADE DE SÃO PAULO.
*    Author: Marco Aurélio Gerosa (gerosa@ime.usp.br)
*    This project was/is sponsored by RNP and FAPESP.
*
*    This file is part of Groupware Workbench (http://www.groupwareworkbench.org.br).
*
*    Groupware Workbench is free software: you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Groupware Workbench is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public License
*    along with Swift.  If not, see <http://www.gnu.org/licenses/>.
*/
package br.org.groupwareworkbench.eureka.hardware;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import br.org.groupwareworkbench.collablet.coord.user.User;
import br.org.groupwareworkbench.core.exceptions.ConfigurationException;
import br.org.groupwareworkbench.core.framework.AbstractBusiness;
import br.org.groupwareworkbench.core.framework.Collablet;
import br.org.groupwareworkbench.core.framework.annotations.ComponentInfo;
import br.org.groupwareworkbench.eureka.knowledge.ConnectionMysql;

@ComponentInfo(
        version = "0.1",
        // configurationURL = "/groupware-workbench/hardware/{hardwareMgr}/configure",
        retrieveURL = "/groupware-workbench/{hardwareMgr}/hardware/{id}"
)
public class HardwareMgrInstance extends AbstractBusiness {
    // private static final String infrawebUrl = "http://infraweb";
    private static final String infrawebUrl = "http://localhost:8080/GW-Application-Eureka/groupware-workbench/hardwareTeste?";

    public HardwareMgrInstance(Collablet collablet) {
        super(collablet);
    }

    public void addMaintenance(Hardware hw, User maintainer) {
        hw.addMaintenance(maintainer);
    }
    
    public void save(Hardware hw){
        hw.setCollablet(getCollablet());
        hw.save();
    }
    
    public Hardware getHardwareById(String id){
        Hardware hardware = Hardware.findById(id);
        if (hardware == null) {
            hardware = new Hardware();
            hardware.setGatiCode(id);
            
            if (updateFromInfraweb(hardware)){
                return hardware;
            }
        }
        return null;
    }

    private String getInfrawebUrl(Hardware hw) {
        if (hw == null || "".equals(hw.getGatiCode())) {
            return null;
        }

        boolean codeIsValid = false;

        ConnectionMysql myConn = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            myConn = new ConnectionMysql();
            conn = myConn.getConnection(ConnectionMysql.INFRAWEB_DB);
            ps = conn.prepareStatement("SELECT CodGati FROM Gati WHERE CodGati = ?");
            ps.setString(1, hw.getGatiCode());
            rs = ps.executeQuery();

            if (rs.next()) {
                codeIsValid = true;
            }

        } catch (Exception e) {
            throw new ConfigurationException(e);

        } finally {
            try {
                if (rs != null) rs.close();
            } catch (Exception e) {
                // TODO
                e.printStackTrace();
            }

            try {
                if (ps != null) ps.close();
            } catch (Exception e) {
                // TODO
                e.printStackTrace();
            }

            try {
                if (conn != null) conn.close();
            } catch (Exception e) {
                // TODO
                e.printStackTrace();
            }

            try {
                if (myConn != null) myConn.close();
            } catch (Exception e) {
                // TODO
                e.printStackTrace();
            }
        }

        if (codeIsValid) {
            return infrawebUrl + "/?" + hw.getGatiCode();
        }
        return null;
    }

    public boolean updateFromInfraweb(Hardware hw) {
        String infrawebHardwareUrl = getInfrawebUrl(hw);

        if (infrawebHardwareUrl == null) return false;
        
        return hw.readDataFromInfraWeb(infrawebHardwareUrl);
    }

}
